package m.jclecai.com.fragment.refreshableList;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.follow.HotOrderDetailActivity;
import m.jclecai.com.adapter.base.DefaultViewHolderAdapter;
import m.jclecai.com.adapter.holder.DefaultViewHolder;
import m.jclecai.com.bean.Response.ResMyPlanList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2017/11/24.
 */

public class MyPlanListFragment extends RefreshableListFragment {
    @Override
    public void requestData() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("user", PreferenceUtil.getUserName(getActivity()));
        paramsMap.put("password", PreferenceUtil.getPassword(getActivity()));

        HttpManager.postAsyn(Constant_Url.MY_PLAN_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                e.printStackTrace();
                mPrListview.onRefreshComplete();
                DisPlay("网络连接异常!!!");
            }

            @Override
            public void onResponse(String response) {
                try {
                    if (!TextUtils.isEmpty(response)) {
                        //这里我们对数据进行要给解析
                        ResMyPlanList resHitList = FastOk.get(response, ResMyPlanList.class);
                        if (resHitList != null && "0".equals(resHitList.getStatus())) {

                            List<ResMyPlanList.PlanBean> hitList = resHitList.getRecords();
                            int totalPages = Integer.parseInt(resHitList.getPages());

                            mAdapter.addAll(hitList);

                            if (mPage < totalPages) {
                                mPage += 1;
                            }
                        } else {
                            DisPlay(resHitList.getMessage());
                        }

                    } else {
                        DisPlay("网络连接异常!!!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    DisPlay("网络连接异常!!!");
                }
                mPrListview.onRefreshComplete();
            }
        }, paramsMap);
    }

    @Override
    protected DefaultViewHolderAdapter createAdapter() {
        return new PlanAdapter(getActivity());
    }


    public class PlanAdapter extends DefaultViewHolderAdapter<ResMyPlanList.PlanBean> {


        public PlanAdapter(Context context) {
            super(context);
        }

        @Override
        public DefaultViewHolder<ResMyPlanList.PlanBean> getRealViewHolder(Context context) {
            return new PlanViewHolder(context, R.layout.item_list_my_plan);
        }
    }

    public class PlanViewHolder extends DefaultViewHolder<ResMyPlanList.PlanBean> {


        @BindView(R.id.tv_date_month)
        TextView mTvDateMonth;

        @BindView(R.id.tv_date_day)
        TextView mTvDateDay;

        @BindView(R.id.tv_lottery_name)
        TextView mTvLotteryName;

        @BindView(R.id.tv_plan_money)
        TextView mTvPlanMoney;

        @BindView(R.id.tv_follow_count)
        TextView mTvFollowCount;

        @BindView(R.id.tv_follow_total_money)
        TextView mTvFollowTotalMoney;

        @BindView(R.id.tv_order_statue)
        TextView mTvOrderStatue;

        public PlanViewHolder(Context context, @LayoutRes int resource) {
            super(context, resource);
        }

        @Override
        public void init(final ResMyPlanList.PlanBean planBean, int position) {

            try {
                Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(planBean.getAddtime());
                String s = (parse.getMonth() + 1) + "月" + parse.getDay() + "日";
                mTvDateMonth.setText((parse.getMonth()+1) + "月");
                mTvDateDay.setText("" + parse.getDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            //mTvOrderPeople.setText("发单人: " + planBean.getUsername());
            mTvLotteryName.setText(planBean.getLotteryname());

            String follow_money = "<html>跟单总金额:<font color='red'>" + planBean.getOne_money() + "</font>元</html>";
            String follow_count = "<html>跟单:<font color='red'>" + planBean.getFollow_count() + "</font>人</html>";
            final String plan_total_money = "<html>总金额:<font color='red'>" + planBean.getFollow_money() + "</font>元</html>";
            //mTvFollowMoney.setText(Html.fromHtml(follow_money));

            mTvFollowTotalMoney.setText(Html.fromHtml(follow_money));
            mTvFollowCount.setText(Html.fromHtml(follow_count));
            mTvPlanMoney.setText(Html.fromHtml(plan_total_money));

            if ("3".equals(planBean.getState())) {
                mTvOrderStatue.setText("已中奖");
            } else if ("1".equals(planBean.getState())) {
                mTvOrderStatue.setText("已撤单");
            } else if ("2".equals(planBean.getState())) {
                mTvOrderStatue.setText("未中奖");
            } else {
                mTvOrderStatue.setText("待开奖");
            }

            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(HotOrderDetailActivity.getIntent(getActivity(),planBean.getId()));
                }
            });

        }
    }
}
